<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/7/3
 * Time: 下午6:17
 */
namespace Backend\Model;

use Think\Model;

class MenuModel extends Model
{
    protected $connection = 'DB_CONFIG1';

    protected $table      = 'menu';

    protected $prefix     = 'tbk_';

    protected $_validate = array(
        array('name', 'require', '菜单名称为必须'), //菜单名称为必须
        array('module_name', 'require', '控制器名称必须'), //控制器名称必须
        array('action_name', 'require', '方法名称必须'), //方法名称必须
    );

    public function getmenu($menu_id)
    {
        $where = array(
            'pid' => $menu_id,
            'display' => 1,
        );
        $order = 'ordid asc,id asc';
        return $this->where($where)->order($order)->select();
    }

    public function admin_menu($pid, $with_self=false) {
        $pid = intval($pid);
        $condition = array('pid' => $pid);
        if ($with_self) {
            $condition['id'] = $pid;
        }
        $condition['display'] = 1;
        $menus = $this->where($condition)->order('ordid asc')->select();
        return $menus;
    }

    public function sub_menu($pid = '', $big_menu = false) {
        $array = $this->admin_menu($pid, false);
        $numbers = count($array);
        if ($numbers==1 && !$big_menu) {
            return '';
        }
        return $array;
    }

    public function get_level($id,$array=array(),$i=0) {
        foreach($array as $n=>$value){
            if ($value['id'] == $id) {
                if($value['pid']== '0') return $i;
                $i++;
                return $this->get_level($value['pid'],$array,$i);
            }
        }
    }

    //删除数据 (软删除)
    public function delete($where, $data)
    {
        return $this->where($where)->save($data);
    }

}